package com.rickpan.entity;

import jakarta.persistence.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

/**
 * 团队密钥实体类
 * 用于端到端加密的密钥管理
 * 
 * @author RickPan Team
 * @since 2025-07-19
 */
@Entity
@Table(name = "team_keys")
@EntityListeners(AuditingEntityListener.class)
public class TeamKey {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "team_id", nullable = false)
    private Long teamId;

    @Column(name = "key_version", nullable = false)
    private Integer keyVersion;

    @Column(name = "encrypted_key", columnDefinition = "TEXT", nullable = false)
    private String encryptedKey; // 使用主密钥加密的团队密钥

    @Column(name = "algorithm", length = 50, nullable = false)
    private String algorithm = "AES-256-GCM";

    @Column(name = "key_length", nullable = false)
    private Integer keyLength = 256;

    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false)
    private KeyStatus status = KeyStatus.active;

    @Column(name = "created_by", nullable = false)
    private Long createdBy; // 创建者ID

    @Column(name = "expires_at")
    private LocalDateTime expiresAt; // 密钥过期时间

    @CreatedDate
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;

    @LastModifiedDate
    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;

    // 关联实体
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "team_id", insertable = false, updatable = false)
    private Team team;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "created_by", insertable = false, updatable = false)
    private User creator;

    // 枚举定义
    public enum KeyStatus {
        active,    // 活跃状态
        expired,   // 已过期
        revoked    // 已撤销
    }

    // 构造函数
    public TeamKey() {}

    public TeamKey(Long teamId, Integer keyVersion, String encryptedKey, Long createdBy) {
        this.teamId = teamId;
        this.keyVersion = keyVersion;
        this.encryptedKey = encryptedKey;
        this.createdBy = createdBy;
    }

    // Getter 和 Setter 方法
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getTeamId() {
        return teamId;
    }

    public void setTeamId(Long teamId) {
        this.teamId = teamId;
    }

    public Integer getKeyVersion() {
        return keyVersion;
    }

    public void setKeyVersion(Integer keyVersion) {
        this.keyVersion = keyVersion;
    }

    public String getEncryptedKey() {
        return encryptedKey;
    }

    public void setEncryptedKey(String encryptedKey) {
        this.encryptedKey = encryptedKey;
    }

    public String getAlgorithm() {
        return algorithm;
    }

    public void setAlgorithm(String algorithm) {
        this.algorithm = algorithm;
    }

    public Integer getKeyLength() {
        return keyLength;
    }

    public void setKeyLength(Integer keyLength) {
        this.keyLength = keyLength;
    }

    public KeyStatus getStatus() {
        return status;
    }

    public void setStatus(KeyStatus status) {
        this.status = status;
    }

    public Long getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(Long createdBy) {
        this.createdBy = createdBy;
    }

    public LocalDateTime getExpiresAt() {
        return expiresAt;
    }

    public void setExpiresAt(LocalDateTime expiresAt) {
        this.expiresAt = expiresAt;
    }

    public LocalDateTime getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }

    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }

    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }

    public Team getTeam() {
        return team;
    }

    public void setTeam(Team team) {
        this.team = team;
    }

    public User getCreator() {
        return creator;
    }

    public void setCreator(User creator) {
        this.creator = creator;
    }

    @Override
    public String toString() {
        return "TeamKey{" +
                "id=" + id +
                ", teamId=" + teamId +
                ", keyVersion=" + keyVersion +
                ", algorithm='" + algorithm + '\'' +
                ", keyLength=" + keyLength +
                ", status=" + status +
                ", createdBy=" + createdBy +
                ", expiresAt=" + expiresAt +
                ", createdAt=" + createdAt +
                '}';
    }
}
